//
//  XGAFNetworkingTools.swift
//  WeIBo
//
//  Created by 苏文潇 on 2017/3/7.
//  Copyright © 2017年 苏文潇. All rights reserved.
//

import UIKit
import AFNetworking

//  网络请求类型
enum RequestType {
    //  get请求
    case get
    //  post请求
    case post
}

//    网络请求工具类
class XGAFNetworkingTools: AFHTTPSessionManager {
//    单例全局访问点
    static let sharedTools: XGAFNetworkingTools = {
        
        let tools = XGAFNetworkingTools()
//        添加可以接受到相应的数据类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
//    MARK: 封装get/post
    func request(type: RequestType, url: String, params: Any?, callBack:@escaping (Any?, Error?)->())
    {
        
        if type == .get
        {
            get(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
            }, failure: { (_, error) in
                callBack(nil, error)
            })
        }
        else
        {
            post(url, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
            }, failure: { (_, error) in
                callBack(nil, error)
            })
        }
    }
    
    
////   MARK: OAuth授权相关的登录
//    func OAuthLoad(code: String, callBack:@escaping (Any?, Error?)->()){
//        
//        let url = "https://api.weibo.com/oauth2/access_token"
//        
//        let parmas = [
//            "client_id": appKey,
//            "client_secret": AppSecret,
//            "grant_type":"authorization_code",
//            "code":code,
//            "redirect_uri":Redirect_uri
//        ]
//        
//        request(type: .post, url: url, params: parmas, callBack: callBack)
//        
//    }
    
}

//   MARK: OAuth授权相关的登录
extension XGAFNetworkingTools {
    
//    MARK: 授权登录
    func OAuthLoad(code: String, callBack:@escaping (Any?, Error?)->()){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let parmas = [
            "client_id": appKey,
            "client_secret": AppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":Redirect_uri
        ]
        
        request(type: .post, url: url, params: parmas, callBack: callBack)
        
    }
    
    
//    MARK:  获取用户信息
    func requestUserMessage(accessToken: String, uid: Int64, callBack: @escaping (Any?, Error?) -> ()){
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let parmas: [String: Any] = [
        
            "access_token":accessToken,
            "uid":uid
        ]
        
        request(type: .get, url: url, params: parmas, callBack: callBack)
    }
    
}

//  MARK: -- 首页相关的微博接口
extension XGAFNetworkingTools {
    
    func statusMessage(callBack: @escaping (Any?, Error?) -> ()){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params = [
            "access_token":XGUserAccountViewModel.sharedUserAccount.userAccount?.access_token
        ]
        
        request(type: .get, url: url, params: params, callBack: callBack)
    }
    
    
    
}






